﻿<!DOCTYPE html>
<html>
<head>
	<!-- 移动相机的位置-->
	<meta charset="UTF-8">
	<title>Three框架</title>
	<script src="js/Three.js"></script>
	<script src="js/Stats.js"></script>
	<script src="js/tween.min.js"></script>

	<style type="text/css">
		div#canvas-frame {
			border: none;
			cursor: pointer;
			width: 100%;
			height: 600px;
			background-color: #EEEEEE;
		}

	</style>
	<script>
        /*
        * 立方体的大小是100,100,100
        *  以立方体上的点 50,50,-50 为中心，绕y轴旋转
        */


        var renderer;
        var stats;

        function initThree() {
            width = document.getElementById('canvas-frame').clientWidth;
            height = document.getElementById('canvas-frame').clientHeight;
            renderer = new THREE.WebGLRenderer({
                antialias : true
            });
            renderer.setSize(width, height);
            document.getElementById('canvas-frame').appendChild(renderer.domElement);
            renderer.setClearColor(0xFFFFFF, 1.0);

            stats = new Stats();
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';
            document.getElementById('canvas-frame').appendChild(stats.domElement);
        }

        var camera;
        function initCamera() {
            camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
            camera.position.x = 0;
            camera.position.y = 0;
            camera.position.z = 600;
            camera.up.x = 0;
            camera.up.y = 1;
            camera.up.z = 0;
            camera.lookAt({
                x : 0,
                y : 0,
                z : 0
            });
        }

        var scene;
        function initScene() {
            scene = new THREE.Scene();
        }

        var light;
        function initLight() {
            light = new THREE.AmbientLight(0xFF0000);
            light.position.set(100, 100, 200);
            scene.add(light);

        }

        var cube;
        var mesh;
        var group;
        function initObject() {



            var geometry = new THREE.BoxGeometry( 100, 100, 100 );

            for ( var i = 0; i < geometry.faces.length; i += 2 ) {

                var hex = Math.random() * 0xffffff;
                geometry.faces[ i ].color.setHex( hex );
                geometry.faces[ i + 1 ].color.setHex( hex );

            }

            var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors} );
            mesh = new THREE.Mesh( geometry,material);
            mesh.position = new THREE.Vector3(0,0,0);

            group = new THREE.Group();
            group.add(mesh);

            mesh.translateX(-50);
            mesh.translateY(-50);
            mesh.translateZ(50);

            scene.add(group);


        }

        function initGrid(){
            var helper = new THREE.GridHelper( 1000, 50 );
            helper.setColors( 0x0000ff, 0x808080 );
            scene.add( helper );
        }

        function threeStart() {
            initThree();
            initCamera();
            initScene();
            initLight();
            initObject();
            initGrid();
            animation();

        }

        function animation()
        {
            group.rotation.y +=0.01;
            renderer.render(scene, camera);
            requestAnimationFrame(animation);

        }

	</script>
</head>

<body onload="threeStart();">
<div style="margin-left:150px;">
	<p>围绕Y轴旋转 方法一：mesh.rotation.y +=0.01;</p>
	<p>方法二：mesh.rotateY(0.1)</p>
</div>
<div id="canvas-frame"></div>
</body>
</html>